//
//  WhiteDetailTableCell.m
//  Bees
//
//  Created by NowYouCanSee on 2019/2/23.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "WhiteDetailTableCell.h"
@implementation WhiteContentModel

@end

@implementation WhiteDetailModel
- (NSMutableArray<WhiteDappInfo*>*)whites{
    if (!_whites) {
        _whites = [NSMutableArray array];
    }
    return _whites;
}
- (NSMutableArray<WhiteContentModel*>*)rowHeights{
    if (!_rowHeights) {
        _rowHeights = [NSMutableArray array];
    }
    return _rowHeights;
}

@end
@interface WhiteDetailCell : XHBaseTableCell
@property (nonatomic,strong) WhiteDappInfo*whiteInfo;
@property (nonatomic,strong) WhiteContentModel*contentM;
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UILabel *nameLab;
@property (nonatomic,strong) UIButton *cancelBtn;
@property (nonatomic,strong) UILabel *contentLab;
@property (nonatomic,strong) UIView *freeBackView;
@property (nonatomic,strong) UILabel *freeLab;
@property (nonatomic,strong) UIButton *freeCancelBtn;
@property (nonatomic,copy) void(^cancelFree)(WhiteDappInfo *white);
@property (nonatomic,copy) void(^cancelWhite)(WhiteDappInfo *white);
@end

@implementation WhiteDetailCell
- (void)setContentM:(WhiteContentModel *)contentM{
    _contentM = contentM;
    self.contentLab.text = contentM.content;
}
- (void)setWhiteInfo:(WhiteDappInfo *)whiteInfo{
    _whiteInfo = whiteInfo;
    self.nameLab.text = whiteInfo.name;
    self.freeBackView.hidden = !whiteInfo.free;
    [self.freeBackView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(whiteInfo.free?AdaptedWidth(50):0.01);
    }];
}

- (void)setupUI{
    self.backView = [UIView new];
    self.backView.backgroundColor = UIColorHex(#F7F7F7);
    self.backView.layer.cornerRadius = AdaptedWidth(13);
    [self.contentView addSubview:self.backView];
    self.nameLab = [UILabel new];
    self.nameLab.textColor = UIColorHex(#FFCC03);
    self.nameLab.font = kDefaultFont(15);
    [self.backView addSubview:self.nameLab];
    self.cancelBtn = [[UIButton alloc]init];
    [self.cancelBtn setBackgroundImage:[UIImage imageWithColor:UIColorHex(#FFCC03)] forState:UIControlStateNormal];
    [self.cancelBtn setTitle:XLocalized(@"取消") forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = kDefaultFont(12);
    [self.backView addSubview:self.cancelBtn];
    ViewRadius(self.cancelBtn,AdaptedWidth(15));

    self.freeBackView = [UIView new];
    [self.backView addSubview:self.freeBackView];
    self.freeLab = [UILabel new];
    self.freeLab.textColor =UIColorHex(#FFCC03);
    self.freeLab.font = kDefaultFont(15);
    [self.freeBackView addSubview:self.freeLab];
    self.freeLab.text = XLocalized(@"免弹窗");
    
    self.freeCancelBtn = [[UIButton alloc]init];
    [self.freeCancelBtn setBackgroundImage:[UIImage imageWithColor:UIColorHex(#FFCC03)] forState:UIControlStateNormal];
    [self.freeCancelBtn setTitle:XLocalized(@"取消") forState:UIControlStateNormal];
    [self.freeCancelBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.freeCancelBtn.titleLabel.font = kDefaultFont(12);
    [self.freeBackView addSubview:self.freeCancelBtn];
    ViewRadius(self.freeCancelBtn,AdaptedWidth(15));
    
    self.contentLab = [UILabel new];
    self.contentLab.textColor = UIColorHex(#333333);
    self.contentLab.font = kDefaultFont(12);
    self.contentLab.numberOfLines = 0;
    [self.backView addSubview:self.contentLab];
    kWeakSelf(self);
    [self.cancelBtn addTapBlock:^(UIButton * _Nonnull btn) {
        !weakself.cancelWhite?:weakself.cancelWhite(weakself.whiteInfo);
    }];
    [self.freeCancelBtn addTapBlock:^(UIButton * _Nonnull btn) {
        !weakself.cancelFree?:weakself.cancelFree(weakself.whiteInfo);
    }];
}
- (void)layoutSubviews{
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(AdaptedWidth(15));
        make.right.mas_equalTo(AdaptedWidth(-15));
    }];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(15));
        make.left.mas_equalTo(AdaptedWidth(15));
    }];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLab.mas_centerY);
        make.right.mas_equalTo(AdaptedWidth(-15));
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(60), AdaptedWidth(30)));
    }];
    [self.freeBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
//        make.height.mas_equalTo(AdaptedWidth(50));
    }];
    [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(15));
        make.right.mas_equalTo(AdaptedWidth(-15));
        make.top.equalTo(self.cancelBtn.mas_bottom).offset(AdaptedWidth(5));
        make.bottom.equalTo(self.freeBackView.mas_top);
    }];
    [self.freeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(AdaptedWidth(15));
    }];
    [self.freeCancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.freeLab.mas_centerY);
        make.right.mas_equalTo(AdaptedWidth(-15));
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(60), AdaptedWidth(30)));
    }];
}
@end

@interface WhiteDetailTableCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UITableView*tableView;
@property (nonatomic,strong) UILabel *nameLab;
@property (nonatomic,strong) UILabel *nameInfoLab;
@property (nonatomic,strong) UIButton *bottomBtn;
@end

@implementation WhiteDetailTableCell
- (void)setDetailModel:(WhiteDetailModel *)detailModel{
    _detailModel = detailModel;
    self.nameLab.text = XLocalized(@"合约名称");
    self.nameInfoLab.text = detailModel.contract_name;
    [self.tableView reloadData];
}
- (void)setupUI{
    self.backView = [UIView new];
    self.backView.backgroundColor = UIColor.whiteColor;
    self.backView.layer.cornerRadius = AdaptedWidth(13);
    [self.backView setLayerShadow:kShadowColor offset:CGSizeMake(0, 0) radius:6];
    [self.contentView addSubview:self.backView];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    ViewRadius(self.tableView, AdaptedWidth(13));
    [self.backView addSubview:self.tableView];
    self.nameLab = [UILabel new];
    self.nameLab.textColor = UIColorHex(333333);
    self.nameLab.font = kBoldFont(17);
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-AdaptedWidth(30), AdaptedWidth(50))];
    [header addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(AdaptedWidth(17));
        make.centerY.mas_equalTo(0);
    }];
    self.nameInfoLab = [UILabel new];
    self.nameInfoLab.textColor = UIColorHex(333333);
    self.nameInfoLab.font = kBoldFont(17);
    [header addSubview:self.nameInfoLab];
    [self.nameInfoLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(AdaptedWidth(-17));
        make.centerY.mas_equalTo(0);
    }];
    self.tableView.tableHeaderView = header;
    UIView *footV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth-AdaptedWidth(30), AdaptedWidth(80))];
    XThemeFlatButton *btn = [XThemeFlatButton button];
    [btn setTitle:XLocalized(@"取消白名单") forState:UIControlStateNormal];
    [footV addSubview:btn];
    btn.frame = CGRectMake(AdaptedWidth(15), AdaptedWidth(20), footV.width-AdaptedWidth(30), AdaptedWidth(40));
    self.tableView.tableFooterView = footV;
    self.tableView.delegate = self;
    self.tableView.dataSource= self;
    
    kWeakSelf(self);
    [btn addTapBlock:^(UIButton * _Nonnull btn) {
        !weakself.cancelAllWhite?:weakself.cancelAllWhite(weakself.detailModel);
    }];
}

- (void)layoutSubviews{
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(10));
        make.left.mas_equalTo(AdaptedWidth(15));
        make.right.mas_equalTo(AdaptedWidth(-15));
        make.bottom.mas_equalTo(AdaptedWidth(-15));
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.mas_equalTo(0);
    }];
   
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.detailModel.whites.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WhiteDetailCell *cell = [WhiteDetailCell cellForTableView:tableView];
    cell.whiteInfo = [self.detailModel.whites objectOrNilAtIndex:indexPath.row];
    cell.contentM = [self.detailModel.rowHeights objectOrNilAtIndex:indexPath.row];
    kWeakSelf(self);
    cell.cancelFree = ^(WhiteDappInfo *white) {
        !weakself.cancelFree?:weakself.cancelFree(white);
    };
    cell.cancelWhite = ^(WhiteDappInfo *white) {
        !weakself.cancelWhite?:weakself.cancelWhite(white);
    };
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self.detailModel.rowHeights objectAtIndex:indexPath.row].height;
}
@end



